//
//  RemoteLocation.m
//  gdemm
//
//  Created by 乔明超 on 16/5/26.
//  Copyright © 2016年 Goldencis. All rights reserved.
//

#import "RemoteLocation.h"
#import "LocationSensor.h"
#import "UserDefaultsUtil.h"
#import "TimeUtil.h"
#import "ApiSupport.h"
#import "HttpUtil.h"
#import "ToastManager.h"
#import "ObjCHiredis.h"
#import "HeartBeat.h"
@interface RemoteLocation()<OnLocationListenerDelegate>
@property (strong,nonatomic) LocationSensor *sendor;
@property (assign,nonatomic) CGFloat mLatitude; //维度
@property (assign,nonatomic) CGFloat mLongtitude;//经度
@property (assign,nonatomic) CGFloat mRadius;//精确度
@property (copy,nonatomic) NSString *mDescription;//地理位置描述

@end
@implementation RemoteLocation
//- (LocationSensor *)sendor{
//    if (_sendor == nil) {
//        _sendor = [LocationSensor getInstance];
//        
//    }
//    return _sendor;
//}
static RemoteLocation *_mInstance;
+ (instancetype)getInstance{
    if (_mInstance == nil) {
        _mInstance = [[RemoteLocation alloc]init];
    }
    return _mInstance;
}

/*提交本地定位的信息到服务器*/
- (void)commitToServer{
    NSMutableDictionary *infoItem = [NSMutableDictionary dictionary];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0 ), ^{
      
        [infoItem setObject:[[UserDefaultsUtil shareInstance]getString:LC_USERGUID] forKey:@"userGUID"];
        //_mLatitude
        [infoItem setObject:[NSNumber numberWithFloat:self.mLatitude] forKey:@"latitude"];
        //_mLongtitude
        [infoItem setObject:[NSNumber numberWithFloat:self.mLongtitude] forKey:@"longitude"];
        //_mRadius
        [infoItem setObject:[NSNumber numberWithFloat:self.mRadius] forKey:@"percision"];
        //_mDescription
        [infoItem setObject:self.mDescription forKey:@"remark"];
        [infoItem setObject:[TimeUtil currentTime] forKey:@"locTime"];
        //HRLog(@"%@",infoItem);
        NSString *url = [ApiSupport submitLocationInfoUrl];
        if (url != nil) {
            NSDictionary *jsonDict = [HttpUtil postWithUrl:url withDict:infoItem withType:@"locationData"];
            if (jsonDict != nil) {
                [self parseResponse:jsonDict];
            }
            
        }
    });
}
/*开启定位*/
- (void)startLocation{
    _sendor = [LocationSensor getInstance];
    [_sendor registerListener:self];
    [self.sendor requestLocationWithMode:LOCATION_TYPE_GPS];
    
}
/*结束定位*/

- (void)stopLocation{
    [_sendor unRegisterListener:self];
}

/*解析提交本地信息之后返回的数据信息*/
- (void)parseResponse:(NSDictionary *)dict{
    NSString *result = dict[@"result"];
    if ([result isEqualToString:@"success"]) {
        [self stopLocation];
        HRLog(@"位置信息提交成功");
    }else{
    
        NSString *errCode = dict[@"errCode"];
        if ([errCode isEqualToString:@"1"]) {
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyUserGUId", @"用户GUID为空")];
        }else if ([errCode isEqualToString:@"2"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyLatitude", @"纬度为空")];
        }else if ([errCode isEqualToString:@"3"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyLongitude", @"经度为空")];
        }else if ([errCode isEqualToString:@"4"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyPrecision", @"精确度为空")];
        }else if ([errCode isEqualToString:@"5"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyPositionDescription", @"位置描述为空")];
        }else if ([errCode isEqualToString:@"6"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyPositionTime", @"定位时间为空")];
        }else if ([errCode isEqualToString:@"7"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_errorSql", @"sql错误")];
        }else if ([errCode isEqualToString:@"8"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_userNotFound", @"用户不存在")];
        }else if ([errCode isEqualToString:@"9"]){
            [ToastManager showToastWindowWithLable:[UIApplication sharedApplication].delegate.window :NSLocalizedString(@"str_emptyJsonData", @"json数据为空")];
        }
    }
}

- (void)locationSensor:(LocationSensor *)locationSensor onReceiveLocation:(Location *)location{
    self.mLatitude = location.mLatitude;
    self.mLongtitude = location.mLongitude;
    self.mRadius = location.mRadius;
    self.mDescription = location.mAddrDescription;
    
    NSLog(@"mLatitude = %f,mLongtitude = %f",self.mLatitude,self.mLongtitude);
    ObjCHiredis *redis = [ObjCHiredis redis:[[UserDefaultsUtil shareInstance] getString:LC_SERVADDR] on:[NSNumber numberWithInt:REDIS_PORT]];
    if (redis != nil) {
        NSString *key = [NSString stringWithFormat:@"%@_remotePosition",[[UserDefaultsUtil shareInstance] getString:LC_USERGUID]];
        NSString *value = [NSString stringWithFormat:@"{\"latitude\":%f,\"longitude\":%f}",self.mLatitude,self.mLongtitude ];
        NSString *command = [NSString stringWithFormat:@"set %@ %@",key,value];
        [redis command:command];
    }
    [self stopLocation];
    //[self commitToServer];
}
@end
